//
//  TableView+Tips.swift
//  CodingTest
//
//  Created by LYPC on 2021/11/18.
//

import UIKit

/// tabviewfooter 没有更多数据
fileprivate let CommonNoMoreDataTips = "已全部加载完毕"
/// 网络错误默认页面显示文字
fileprivate let CommonEmptyTipsError = "加载失败，点击重新加载..."

fileprivate let emptyTipAttrs: [NSAttributedString.Key : Any]  = {
    let paragraph = NSMutableParagraphStyle()
    paragraph.alignment = .center
    return [.font:  UIFont.regular(14),
            .foregroundColor: UIColor.color(.tertiary),
            .paragraphStyle: paragraph]
}()

fileprivate let errorTipAttrs: [NSAttributedString.Key : Any]  = {
    let paragraph = NSMutableParagraphStyle()
    paragraph.alignment = .center
    return [.font:  UIFont.regular(14),
            .foregroundColor: UIColor.color(.tertiary),
            .paragraphStyle: paragraph]
}()

/// 刷新footer noreMore
fileprivate let attrNoMoreTips: NSAttributedString = {
    return CommonNoMoreDataTips.attributedStr(
                                font: .regular(13),
                                color: UIColor.color(.tertiary),
                                lineAlignment: .center)
}()

extension CTRefreshTableView {
    
    func addHeaderFooterRefresh(noreMoreTips: NSAttributedString? = nil) {
        guard let vm = vm else { return }
        self.addRefreshHeader {  vm.refreshList() }
        if let tips = noreMoreTips {
            self.addRefreshFooter(tips) {  vm.loadMoreList() }
        } else {
            self.addRefreshFooter(attrNoMoreTips) {  vm.loadMoreList() }
        }
    }
    
    func addFooter(noreMoreTips: NSAttributedString? = nil) {
        guard let vm = vm else { return }
        if let tips = noreMoreTips {
            self.addRefreshFooter(tips) {  vm.loadMoreList() }
        } else {
            self.addRefreshFooter(attrNoMoreTips) {  vm.loadMoreList() }
        }
    }
    
    func addEmpty(tipStr: String,
                  type: ScrollViewErrorType = .empty,
                  topY: CGFloat = 0,
                  offsetY: CGFloat? = nil,
                  callback: (() -> Void)? = nil) {
        let tipsAttr = NSAttributedString(string: tipStr, attributes: errorTipAttrs)
        addEmpty(attrTip: tipsAttr, type: type, topY: topY, offsetY: offsetY, callback: callback)
    }
    
    func addError(tips: String? = nil,
                  topY: CGFloat = 0,
                  offsetY: CGFloat? = nil,
                  callback: (() -> Void)? = nil) {
        let str = tips ?? CommonEmptyTipsError
        let tipsAttr = NSAttributedString(string: str, attributes: errorTipAttrs)
        self.setError(tips: tipsAttr) { [weak self]() in
            self?.hideError()
            self?.vm?.start2LoadData()
            callback?()
        }
        if let offsetY = offsetY
        { self.placeError(topY: topY, offsetY: offsetY)}
    }
}

extension UITableView {
    
    ///  给 tableView 添加 refreshFooter
    public func addRefreshFooter(_ noreMoreTips: NSAttributedString? = nil, block:@escaping () -> Void) {
        if let tips = noreMoreTips {
            addRefreshFooterV(tips, block: block)
        } else {
            addRefreshFooterV(attrNoMoreTips, block: block)
        }
    }
}

public extension ScrollViewAnimateProtocol {

    func addEmpty(tips: String,
                  type: ScrollViewErrorType = .empty,
                  topY: CGFloat = 0,
                  offsetY: CGFloat? = nil,
                  callback: (() -> Void)? = nil) {
        let tipsAttr = NSAttributedString(string: tips, attributes: emptyTipAttrs)
        baseView.setEmpty(type: type, tips: tipsAttr, callback: callback)
        if let offsetY = offsetY
        { baseView.placeEmpty(topY: topY, offsetY: offsetY) }
    }
    
    func addError(tips: String? = nil,
                  topY: CGFloat = 0,
                  offsetY: CGFloat? = nil,
                  callback: (() -> Void)? = nil) {
        let str = tips ?? CommonEmptyTipsError
        let tipsAttr = NSAttributedString(string: str, attributes: errorTipAttrs)
        baseView.setError(tips: tipsAttr, callback: callback)
        if let offsetY = offsetY { baseView.placeError(topY: topY, offsetY: offsetY) }
    }
}

